package weiyao.xinxidasai.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import weiyao.xinxidasai.DTO.Result;
import weiyao.xinxidasai.DTO.versionDTO;
import weiyao.xinxidasai.Utils.ThreadLocal.UserHolder;
import weiyao.xinxidasai.service.VersionService;

import java.util.List;

@RestController
@RequestMapping("/versions")
public class VersionController {

    @Autowired
    private VersionService versionService;

    // 新增Version
    @PostMapping("/createversion")
    public Result createVersion() {
        return  versionService.createVersion();
    }

    // 查找version
    @GetMapping("/find/{versionNo}")
    public List<versionDTO> getVersionById(@PathVariable String versionNo) {

        return versionService.getVersionById(versionNo);

    }

    // 查询用户所有Version
    @GetMapping
    public List<versionDTO> getAllVersionsByUserId() {
        return versionService.getAllVersionsByUserId(UserHolder.getUser().getUserId());
    }
   

    // 更新Version
    @PostMapping("update")
    public Result updateVersion(@RequestParam("versionId") Long versionId,@RequestParam("versionNo") String  versionNo) {
        System.out.println("versionId = " + versionId);
        System.out.println("versionNo = " + versionNo);

        return  versionService.updateVersion(versionId,versionNo);
    }

    // 删除Version
    @DeleteMapping("/{versionId}")
    public Result deleteVersionById(@PathVariable Long versionId) {
       return  versionService.deleteVersionById(versionId) ;
    }
}

